/**
 *  封装socket.io 为了获取server以便监听
 *  Created by lihao on 2018-04-11
 */
let socket_io_temp = {};
let num = [];
let socket_io = require('socket.io');

// 获取io
socket_io_temp.getSocketio = function(server){
  let io = socket_io.listen(server);

    /**
     *   将io对象放在全局变量上
     */
    global._bySocketIo = io;

  io.sockets.on('connection',function(socket){

      /**
       *   将socket对象放到全局变量window对象放上面去
       */

      global._bySocket = socket;
      global._bySocket123 = 111;

      num.push(socket.client.conn.id);

      socket.on('update',function(){
          socket.broadcast.emit('number', { number: num.length });
      });
      console.log('appId',socket.id);

      socket.broadcast.emit('number', { number: num.length });
      console.log('a user connection');


      socket.emit('line', { status: 'you on line' });


      socket.on('send',function(data){
          console.log(data);

          data.name=data.name===''?socket.id:data.name;
          data.flag= 0;

          socket.broadcast.emit('message',{data:data})  // 向除了本身的所有
          // io.emit('message',{data:data})             // 所有包括自己
      })
  })
};
module.exports = socket_io_temp;